#include "cell_usermgr.h"
#include "ui_cell_usermgr.h"

#include <QFileDialog>

Cell_UserMgr::Cell_UserMgr(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Cell_UserMgr)
{
    ui->setupUi(this);

    ui->tableView->setModel(&m_model);
    //设置tableView无法编辑
    ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
    //设置tableView只能选择单行
    ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);


/*
#if 0
    for(int i = 0; i < 10; i++){
        QList<QStandardItem *> items;
        items.append(new QStandardItem("100" + QString::number(i)));
        items.append(new QStandardItem(QString::number(i) + "年一班"));
        items.append(new QStandardItem("计算机系" + QString::number(i)));
        items.append(new QStandardItem("zhangsan" + QString::number(i)));
        items.append(new QStandardItem("11111" + QString::number(i)));
        items.append(new QStandardItem("zhangsan" + QString::number(i)));
        items.append(new QStandardItem("学生"));
        m_model.appendRow(items);
    }
#endif
*/

#if 1
    initPage();
#endif


}

Cell_UserMgr::~Cell_UserMgr()
{
    delete ui;
}


void Cell_UserMgr::initPage()
{
    m_model.clear();

    //设置表头
    m_model.setHorizontalHeaderLabels(QStringList{"用户ID", "年级", "部门", "用户名", "密码", "昵称", "权限"});


    QString sql = QString(" where 1=1 ");
    QString strCondition = ui->le_condition->text();
    if("" != strCondition){
        sql.append(QString(" and username='%1'").arg(strCondition));
    }

    QVector<QStringList> userList = SqlMgr::getInstance()->getUsers(sql);
    int rows = userList.size();

    /*
    for(int i = 0; i < rows; i++){
        QList<QStandardItem *> items;
        items.append(new QStandardItem(userList[i][0]));
        items.append(new QStandardItem(userList[i][1]));
        items.append(new QStandardItem(userList[i][2]));
        items.append(new QStandardItem(userList[i][3]));
        items.append(new QStandardItem(userList[i][4]));
        items.append(new QStandardItem(userList[i][5]));
        items.append(new QStandardItem(userList[i][6]));
        m_model.appendRow(items);
    }
    */

    for(int i = 0; i < rows; i++){
        QList<QStandardItem *> items;
        int cols = userList[i].size();
        for(int j = 0; j < cols; j++){
            items.append(new QStandardItem(userList[i][j]));
        }
        m_model.appendRow(items);
    }
}

void Cell_UserMgr::on_btn_query_clicked()
{

    this->initPage();
}

void Cell_UserMgr::on_le_condition_returnPressed()
{
    this->initPage();
}

void Cell_UserMgr::on_btn_delete_clicked()
{
    QModelIndex modelIndex = ui->tableView->currentIndex();
    int currentRowIndex = modelIndex.row();
    if(currentRowIndex < 0){
        QMessageBox::warning(this, "提示", "请选中一行记录");
        return;
    }
    qDebug() << "当前选中的行号:" << currentRowIndex;

    QStringList list = this->m_model.data(modelIndex).toStringList();
    qDebug() << list;

    QStandardItem * item = this->m_model.item(currentRowIndex, 0);
    QString useridStr = item->text();

    int userid = useridStr.toInt();
    qDebug() << userid;

    SqlMgr::getInstance()->delUser(useridStr);
    initPage();

}

void Cell_UserMgr::on_btn_import_clicked()
{
    QString filePath = QFileDialog::getOpenFileName(this, "请选择txt文件", ".", "*.txt");

    if("" == filePath){
        QMessageBox::critical(this, "错误", "未选择一个txt文件");
        return;
    }

    QFile file(filePath);
    bool openFlag = file.open(QIODevice::ReadOnly);
    if(!openFlag){
        QMessageBox::critical(this, "错误", "文件打开失败!");
        return;
    }

    QVector<QStringList> vector;

    while(!file.atEnd()){
        QString line = file.readLine();
        QStringList valueList = line.split(",");
        vector.push_back(valueList);
    }

    SqlMgr::getInstance()->addUser(vector);

    initPage();

}
























